/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.util;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

    public static final String USERNAME = "nttung1991";
    public static final String PASSWORD = "11011991";

    /**
     * 
     * @param mailFrom: From mail address
     * @param mailTo: To mail address
     * @param mailSubject: Mail subject
     * @param mailText: Mail content 
     * @throws Exception 
     */
    public void sendMail(String mailFrom, String mailTo,
            String mailSubject, String mailText)
            throws Exception {

        Properties config = createConfiguration();


        Session session = Session.getInstance(config, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        SendMail.USERNAME,
                        SendMail.PASSWORD);
            }
        });


        Message message = new MimeMessage(session);
        message.setSentDate(new Date());
        message.setFrom(new InternetAddress(mailFrom));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        message.setSubject(mailSubject);
        message.setText(mailText);


        Transport.send(message);
    }

    /**
     * 
     * @return Property
     */
    private Properties createConfiguration() {
        return new Properties() {

            {
                put("mail.smtp.host", "smtp.gmail.com");
                put("mail.smtp.auth", "true");
                put("mail.smtp.port", "465");
                put("mail.smtp.socketFactory.port", "465");
                put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            }
        };
    }
}